<template>
  <el-dialog title="提示" :visible.sync="showModify" width="30%" center>
    <el-form ref="form" :model="formData" label-width="80px" :rules="validateRules">
      <el-form-item label="公司名称" prop="groupName">
        <el-input v-model="formData.groupName" placeholder="请输入公司名称" maxlength="50" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="备注描述">
        <el-input
          v-model="formData.groupRemark"
          type="textarea"
          :rows="2"
          placeholder="公司描述"
          maxlength="100"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="父机构">
        <el-input
          v-model="formData.parentID"
          type="textarea"
          :rows="2"
          placeholder="请输入父机构"
          maxlength="100"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onModify">确定</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      showModify: false,
      formData: {},
      validateRules: {
        groupName: [
          { required: true, message: "请输入公司名称", trigger: "blur" }
        ],
        groupRemark: [
          { required: true, message: "请输入公司描述", trigger: "blur" }
        ],
        parentID: [
          { required: true, message: "请输入该公司父机构", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    ...mapActions("pms/base.group", ["addGroup", "updateGroup"]),
     showDialog(data = {}) {
      this.formData = data;
      this.showModify = true;
    },
   onModify() {
      console.log("onModify");
      console.log(this.formData);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formData.id) {
            this.updateGroup(this.formData).then(() => {
              this.showModify = false;
              this.$emit("change");
              this.$message.success("修改成功");
            });
          } else {
            this.addGroup(this.formData).then(() => {
              this.showModify = false;
              this.$emit("change");
              this.$message.success("添加成功");
            });
          }
        } else {
          console.log("验证失败");
        }
      });
    }
  }
};
</script>